package cn.edu.zafu.bigdata.x.yaojy;
import java.io.*;
import java.net.*;

public class Server {
    public static void main(String[] args) {
        int port = 11234  ;
        try (ServerSocket serverSocket = new ServerSocket(port)){
            System.out.println("Server is listening on port" + port);
            while(true){
                try(Socket socket = serverSocket.accept();
                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    PrintWriter out = new PrintWriter(socket.getOutputStream(),true)){
                    System.out.println("New client connected");
                    String inputLine;
                    while ((inputLine = in.readLine()) != null){
                        System.out.println("Received:" + inputLine);
                        out.println("Server received:" + inputLine);
                    }
                }catch(IOException e){
                    System.out.println("Error handing client:" + e.getMessage());
                }
            }
        }catch (IOException e){
            System.err.println("Coule not listen on port" + port);
            System.exit(-1);
        }
    }
}
